package com.hoyo.service;

import java.util.List;

import javax.security.auth.login.AccountNotFoundException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hoyo.common.AppConstant;
import com.hoyo.common.PageInfo;
import com.hoyo.domain.Bullet;
import com.hoyo.domain.BulletReport;
import com.hoyo.domain.Comment;
import com.hoyo.domain.ReportItem;
import com.hoyo.domain.User;
import com.hoyo.domain.UserInfo;
import com.hoyo.domain.UserPermission;
import com.hoyo.mapper.BulletMapper;
import com.hoyo.mapper.UserMapper;
import com.hoyo.utils.MailUtils;
import com.hoyo.utils.MessageUtils;

@Service
public class BulletServiceImpl implements BulletService {

	@Autowired
	private BulletMapper bulletMapper;
	
	@Autowired
	private UserMapper userMapper;

	@Override
	public void save(Bullet bullet) throws Exception {
		bulletMapper.save(bullet);
	}

	@Override
	public void report(String account, int bulletId, String description) throws Exception {
		User user = userMapper.findByIdentifier(account);
		if(user == null){
			throw new AccountNotFoundException();
		}
		bulletMapper.saveReport(new BulletReport(user.getId(), bulletId, description));
	}

	@Override
	public PageInfo<ReportItem> pageFindReportBullets(int pageNum, int pageSize) throws Exception {
		List<ReportItem> reportBullets = bulletMapper.findBulletReportsByCheckOrderByCreateTimeDescLimitBy(false,
				(pageNum - 1) * pageSize, pageSize);
		int total = reportBullets.size() > 0 ? reportBullets.get(0).getTotal() : 0;
		return new PageInfo<>(1, 3, total, reportBullets);
	}

	@Override
	public PageInfo<ReportItem> reportHandle(int id, int userId, int actionType) throws Exception {
		int bulletId = bulletMapper.findBulletIdByReportId(id);
		switch (actionType) {
			case 0:
				bulletMapper.updateIsSolvedAndIsReallyById(id, true, true);
				bulletMapper.deleteById(bulletId);
				this.login4bidAndSendMessage(userId);
				break;
			case 1:
				bulletMapper.updateIsSolvedAndIsReallyById(id, true, true);
				bulletMapper.deleteById(bulletId);
				userMapper.saveUserPermission(new UserPermission(userId, "/bullet/new"));
				break;
			case -1:
				bulletMapper.updateIsSolvedAndIsReallyById(id, true, false);
				break;
	
			default:
				break;
		}
		return pageFindReportBullets(1, 3);
	}

	@Override
	public PageInfo<Bullet> pageFindBullets(int pageNum, int pageSize) throws Exception {
		List<Bullet> bullets = bulletMapper.findBulletsLimitBy((pageNum-1)*pageSize, pageSize);
		int total = bullets.size()>0?bullets.get(0).getTotal():0;
		return new PageInfo<>(pageNum, pageSize, total, bullets);
	}

	@Override
	public PageInfo<Bullet> removeOne(int id) throws Exception {
		bulletMapper.deleteById(id);
		return this.pageFindBullets(1, 10);
	}

	@Override
	public PageInfo<Bullet> login4bid(int userId, int commentId) throws Exception {
		this.login4bidAndSendMessage(userId);
		bulletMapper.deleteById(commentId);
		return this.pageFindBullets(1, 10);
	}

	@Override
	public PageInfo<Bullet> comment4bid(int userId, int commentId) throws Exception {
		userMapper.saveUserPermission(new UserPermission(userId, "/comment/new"));
		bulletMapper.deleteById(commentId);
		return this.pageFindBullets(1, 10);
	}
	
	private void login4bidAndSendMessage(int userId)throws Exception {
		userMapper.updateIsLockByUserId(true, userId);
		User user = userMapper.findById(userId);
		if(user == null){
			throw new AccountNotFoundException();
		}
		UserInfo userInfo = userMapper.findUserInfoByUserId(userId);
		String phone = user.getPhone();
		String nickname = userInfo.getNickname();
		if(phone == null || "".equals(phone)){
//				send email message
			MailUtils.send("hoyo", String.format(AppConstant.MESSAGE_CODE_TEMPLATE, nickname), user.getEmail());
		}else{
//				send telephone message
			String params = "{\"nickname\":\""+nickname+"\"}";
			MessageUtils.send(phone, AppConstant.Message.TEMPLATE_MESSAGE, params);
		}
	}

	@Override
	public Bullet findBulletById(int id) {
		// TODO Auto-generated method stub
		return bulletMapper.findBulletById(id);
	}
}
